package com.ticket.sass.admin.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.ticket.sass.common.entity.tenant.CommoditySkuAttrValueMapEntity;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * SKU属性值关联 Mapper 接口
 * </p>
 *
 * @author ywb
 * @since 2024-03-06
 */
public interface CommoditySkuAttrValueMapMapper extends BaseMapper<CommoditySkuAttrValueMapEntity> {
    @Select(
        """
            SELECT
                map.sku_id,
                attrs.name AS attr_name,
                attr_values.name AS attr_value_name
            FROM
                commodity_sku_attr_value_maps map
            JOIN
                commodity_sku_attrs attrs ON map.attr_id = attrs.id
            JOIN
                commodity_sku_attr_values attr_values ON map.attr_value_id = attr_values.id
            WHERE
                -- 确保所有相关表的记录未被删除，且仅返回给定sku_id的记录
                (map.deleted_at IS NULL AND attrs.deleted_at IS NULL AND attr_values.deleted_at IS NULL) AND map.sku_id IN (${skuIds})
        """)
    List<CommoditySkuAttrValueMapEntity> selectAttrAndValueBySkuIds(@Param("skuIds") String skuIds);

}
